package com.hyg.lib_base.MyView;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.core.content.ContextCompat;


import com.hyg.lib_base.R;

import java.util.ArrayList;
import java.util.List;

/*         健康趋势 人像 系统数据分析图     */
public class HealthSystemView extends View {
    //宽度
    public int mWidth;
    //高度
    public int mHeight;
    //原点坐标
    public int centerX;
    public int centerY;
    //背景圆心坐标
    public float BGcenterX;
    public float BGcenterY;
    //网格半径
    public float radius;
    //数据最大值
    public float angle;
    //颜色值
    public int mTextColor = Color.BLACK;

    public Paint polygonPaint; //边框paint
    public Paint regionColorPaint; //覆盖区域paint

    public String Sex = "男";

    public List<BodyConstitutionPoint> Points = new ArrayList<>();
    public Context context;

    // 画实心圆的画笔
    public Paint mCirclePaint;
    // 画圆环的画笔
    public Paint mRingPaint;
    // 画圆环的画笔背景色
    public Paint mRingPaintBg;
    // 画圆中字体的画笔
    public Paint mTextPaint;
    // 画图下文字的画笔
    public Paint mSystemTextPaint;
    // 圆形颜色
    public int mCircleColor;
    // 圆环颜色
    public int mRingColor;
    // 圆环背景颜色
    public int mRingBgColor;
    // 半径
    public float mRadius;
    // 圆环半径
    public float mRingRadius;
    // 圆环宽度
    private float mStrokeWidth;
    // 圆心x坐标
    private float mXCenter;
    // 圆心y坐标
    private float mYCenter;
    // 圆中字的长度
    private float mTxtWidth;
    // 圆中字的高度
    private float mTxtHeight;
    // 圆外字的长度
    private float mTxtSWidth;
    // 圆外字的高度
    private float mTxtSHeight;
    // 圆外字的文本
    private String mSystemText = "神经系统";
    // 总进度
    private int mTotalProgress = 100;
    // 当前进度
    private int mProgress;

    public HealthSystemView(Context context) {
        super(context);
        this.context = context;
        initAttrs(context, null);
        init(context);
        initVariable();
    }

    public HealthSystemView(Context context,  AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initAttrs(context, attrs);
        init(context);
        initVariable();
    }

    public HealthSystemView(Context context,  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initAttrs(context, attrs);
        init(context);
        initVariable();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mWidth = w;
        mHeight = h;
        //中心坐标
        centerX = mWidth / 2;
        centerY = mHeight / 2;
        radius = (Math.min(mWidth, mHeight) / 2 * 0.8f);
        postInvalidate();
        super.onSizeChanged(w, h, oldw, oldh);
    }
    //属性
    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray typeArray = context.getTheme().obtainStyledAttributes(attrs,
                R.styleable.TasksCompletedView, 0, 0);
        mRadius = typeArray.getDimension(R.styleable.TasksCompletedView_radius, 32);
        mStrokeWidth = typeArray.getDimension(R.styleable.TasksCompletedView_strokeWidth, 12);
        mCircleColor = typeArray.getColor(R.styleable.TasksCompletedView_circleColor, 0xFFFFFFFF);
        mRingColor = typeArray.getColor(R.styleable.TasksCompletedView_ringColor, 0xFF87CEEB);
        mRingBgColor = typeArray.getColor(R.styleable.TasksCompletedView_ringBgColor, 0xFFd3d3d3);
    }
    //初始化画笔
    private void initVariable() {
        mRingRadius = mRadius + mStrokeWidth / 2;
        //内圆
        mCirclePaint = new Paint();
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setColor(mCircleColor);
        mCirclePaint.setStyle(Paint.Style.FILL);

        //外圆弧背景
        mRingPaintBg = new Paint();
        mRingPaintBg.setAntiAlias(true);
        mRingPaintBg.setColor(mRingBgColor);
        mRingPaintBg.setStyle(Paint.Style.STROKE);

        //外圆弧
        mRingPaint = new Paint();
        mRingPaint.setAntiAlias(true);
        mRingPaint.setColor(mRingColor);
        mRingPaint.setStyle(Paint.Style.STROKE);
        mRingPaint.setStrokeCap(Paint.Cap.ROUND);//设置线冒样式，有圆 有方

        //中间字
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setColor(mRingColor);
        mTextPaint.setTextSize(mRadius);

        mSystemTextPaint = new Paint();
        mSystemTextPaint.setAntiAlias(true);
        mSystemTextPaint.setStyle(Paint.Style.FILL);
        mSystemTextPaint.setColor(Color.BLACK);
        mSystemTextPaint.setTextSize(mRadius / 8 * 5);

    } //画图

    private void init(Context context) {
        //        外边框
        polygonPaint = new Paint();
        polygonPaint.setAntiAlias(true);
        polygonPaint.setAlpha(255);
        polygonPaint.setStyle(Paint.Style.FILL);

        //选中区域颜色
        regionColorPaint = new Paint();
        regionColorPaint.setStyle(Paint.Style.STROKE);
        regionColorPaint.setAlpha(150);
        regionColorPaint.setAntiAlias(true);

        BodyConstitutionPoint item1 = new BodyConstitutionPoint("神经系统", 100.0);
        BodyConstitutionPoint item2 = new BodyConstitutionPoint("免疫系统", 100.0);
        BodyConstitutionPoint item3 = new BodyConstitutionPoint("内分泌系统", 100.0);
        BodyConstitutionPoint item4 = new BodyConstitutionPoint("生殖系统", 100.0);
        BodyConstitutionPoint item5 = new BodyConstitutionPoint("循环系统", 100.0);
        BodyConstitutionPoint item6 = new BodyConstitutionPoint("运动系统", 100.0);
        BodyConstitutionPoint item7 = new BodyConstitutionPoint("呼吸系统", 100.0);
        BodyConstitutionPoint item8 = new BodyConstitutionPoint("泌尿系统", 100.0);
        BodyConstitutionPoint item9 = new BodyConstitutionPoint("消化系统", 100.0);
        Points.add(item1);
        Points.add(item2);
        Points.add(item3);
        Points.add(item4);
        Points.add(item5);
        Points.add(item6);
        Points.add(item7);
        Points.add(item8);
        Points.add(item9);
        //        //计算圆心角
//        angle = ( float) (Math.PI / Points.size());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawSex(canvas);
        //画数据线
        drawRegion(canvas);
    }

    private void drawSex(Canvas canvas){
        BGcenterX = getWidth() / 15 * 3;
        BGcenterY = BGcenterX * 1.8f;

        regionColorPaint.setStrokeWidth(BGcenterX * 14 /10);
        LinearGradient backGradient2 = new LinearGradient(0 , getMeasuredHeight()/3,  getMeasuredWidth() , 0 ,
                new int[]{ContextCompat.getColor(context, R.color.gradient_Violet),
                        ContextCompat.getColor(context, R.color.gradient_blue) }, new float[]{0,0.8f} , Shader.TileMode.CLAMP);
        regionColorPaint.setShader(backGradient2);
        canvas.drawCircle(BGcenterX ,BGcenterY ,BGcenterX * 27 / 10 ,regionColorPaint);

        LinearGradient backGradient1 = new LinearGradient(getMeasuredWidth() / 3 , 0, 0, getMeasuredHeight() ,
                new int[]{ContextCompat.getColor(context, R.color.gradient_blue),
                        ContextCompat.getColor(context, R.color.gradient_Violet)}, new float[]{0,0.8f}, Shader.TileMode.CLAMP);
        polygonPaint.setShader(backGradient1);
        canvas.drawCircle(BGcenterX ,BGcenterY ,BGcenterX * 20 / 10 ,polygonPaint);

        if (Sex.equals("男")) {
            Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(R.mipmap.male)).getBitmap();
            int mBitWidth = bitmap.getWidth();
            int mBitHeight = bitmap.getHeight();
            setImgSize(bitmap,mBitWidth,(int)BGcenterX * 35 / 10);
            canvas.drawBitmap(bitmap,  BGcenterX - mBitWidth/2 ,  BGcenterY - mBitHeight/2, null);
        } else {
            Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(R.mipmap.woman)).getBitmap();
            int mBitWidth = bitmap.getWidth();
            int mBitHeight = bitmap.getHeight();
            setImgSize(bitmap,mBitWidth,(int)BGcenterX * 35 / 10);
            canvas.drawBitmap(bitmap,  BGcenterX - mBitWidth/2 ,  BGcenterY - mBitHeight/2, null);
        }
        canvas.save();
    }

    public Bitmap setImgSize(Bitmap bm, int newWidth, int newHeight) { // 获得图片的宽高.
        int width = bm.getWidth();
        int height = bm.getHeight(); // 计算缩放比例.
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height; // 取得想要缩放的matrix参数.
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight); // 得到新的图片.
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        return newbm;
    }

    @SuppressLint("NewApi")
    private void drawRegion(Canvas canvas) {
        Path path = new Path();
        float r = radius / Points.size(); //每层的间距

        mRadius = BGcenterX / 7 * 2;
        mStrokeWidth = mRadius / 7 * 2 ;
        mRingPaintBg.setStrokeWidth(mStrokeWidth);
        mRingPaint.setStrokeWidth(mStrokeWidth);

        mSystemTextPaint.setTextSize(mRadius / 8 * 5);
        for (int i = 0; i < Points.size(); i++) {
            //计算该数值与最大值比例
//            Double perCenter = Points.get(i).getConversionScore() / 100;
            Double perCenter = 0.95;
            //小圆点所在位置距离圆心的距离
            double perRadius = perCenter * radius;
//            float x = (float) (centerX + Math.sin(angle * i) * (perCenter * (radius - r) + r));
//            float y = (float) (centerY - Math.cos(angle * i) * (perCenter * (radius - r) + r));
            double x,y;
            if( i < 4){
                //计算圆心角
                angle = ( float) (Math.PI * 2 / 20) * (i*2 + 3);
                x = BGcenterX + Math.sin(angle) *(BGcenterX * 20 / 10);
                y = BGcenterY - Math.cos(angle) *(BGcenterX * 20 / 10);
                mXCenter = (float)x;
                mYCenter = (float)y;
                canvas.drawCircle(mXCenter, mYCenter, mRadius, mCirclePaint);
                mRingRadius = mRadius + mStrokeWidth / 2 ;
                mTextPaint.setTextSize(mRadius);
            }else{
                angle = ( float) (Math.PI * 2 / 25) * (i*2 - 3);
                x = BGcenterX + Math.sin(angle) *(BGcenterX * 34 / 10);
                y = BGcenterY - Math.cos(angle) *(BGcenterX * 34 / 10);
                mXCenter = (float)x;
                mYCenter = (float)y;
                canvas.drawCircle(mXCenter, mYCenter, mRadius * 1.1f, mCirclePaint);
                mRingRadius = mRadius * 1.1f + mStrokeWidth / 2 ;
                mTextPaint.setTextSize(mRadius * 1.1f);
            }
            Log.d("angle","angle:"+angle+"  mRadius:"+mRadius+"  mStrokeWidth:"+mStrokeWidth+"  mRingRadius:"+mRingRadius);
            // 外圆弧背景
            RectF oval1 = new RectF();
            oval1.left = (mXCenter - mRingRadius);
            oval1.top = (mYCenter - mRingRadius);
            oval1.right = mRingRadius * 2 + (mXCenter - mRingRadius);
            oval1.bottom = mRingRadius * 2 + (mYCenter - mRingRadius);
            canvas.drawArc(oval1, 0, 360, false, mRingPaintBg);
            // 圆弧所在的椭圆对象、圆弧的起始角度、圆弧的角度、是否显示半径连线
            // 外圆弧
            mProgress = (int)Points.get(i).getConversionScore();
            if (mProgress > 0 ) {
                RectF oval = new RectF();
                oval.left = (mXCenter - mRingRadius);
                oval.top = (mYCenter - mRingRadius);
                oval.right = mRingRadius * 2 + (mXCenter - mRingRadius);
                oval.bottom = mRingRadius * 2 + (mYCenter - mRingRadius);
                canvas.drawArc(oval, -90, ((float)mProgress / mTotalProgress) * 360, false, mRingPaint); // //字体
                Paint.FontMetrics fm = mTextPaint.getFontMetrics();
                mTxtHeight = (int) Math.ceil(fm.descent - fm.ascent);
                String txt = mProgress + "";
                mTxtWidth = mTextPaint.measureText(txt, 0, txt.length());
                canvas.drawText(txt, mXCenter - mTxtWidth / 2, mYCenter + mTxtHeight / 4, mTextPaint);
            }

            //TextView换行的原理工具StaticLayout
//            TextPaint textPaint = new TextPaint();
//            textPaint.setARGB(0xBB, 0, 0, 0);
//            textPaint.setTextSize(getResources().getDimension(R.dimen.font13));
//            textPaint.setAntiAlias(true);
//            StaticLayout layout = new StaticLayout(txt, textPaint, (int) Math.ceil(StaticLayout.getDesiredWidth(txt, textPaint)),
//            Layout.Alignment.ALIGN_CENTER, 1.0F, 0.0F, true);
//            canvas.translate(x-getResources().getDimension(R.dimen.font33), y-(getResources().get
            // Dimension(R.dimen.font33) / 2));//从某个小圆的 起始坐标开始画
//            layout.draw(canvas);
            Paint.FontMetrics fms = mSystemTextPaint.getFontMetrics();
            mTxtSHeight = (int) Math.ceil(fms.descent - fms.ascent);
            mTxtSWidth = mSystemTextPaint.measureText(mSystemText, 0, mSystemText.length());
            mSystemText = Points.get(i).getBodyConstitutionTypes();
            canvas.drawText(mSystemText, mXCenter - mTxtSWidth / 2, mYCenter + mRingRadius + mTxtSHeight + mStrokeWidth / 2 , mSystemTextPaint);

            canvas.save();
            canvas.restore();//别忘了restore

//            mTextPaint.setTextAlign(Paint.Align.CENTER);
//            canvas.drawText(txt, x, y , mTextPaint);
        }
        path.close();
        //        canvas.drawPath(path, regionColorPaint);
    }

    /**
     * 设置文本颜色
     *
     * @param color
     */
    public void setTextColor(int color) {
        mTextColor = color;
        mTextPaint.setColor(mTextColor);
        postInvalidate();
    }

    //设置数值
    public void setData(List<BodyConstitutionPoint> Points, String sex) {
        this.Points = Points;
        this.Sex = sex;

        postInvalidate();
    }


}